(function(){
    "use strict";
    app.controller('hostDetailCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','LANGUAGE','eChartLineFactory','$interval',
        function($rootScope, $scope,$state,httpLoad,$stateParams,LANGUAGE,eChartLineFactory,$interval) {
        /* 变量声明
         ---------------------------------------*/
        var id = $stateParams.id,
        	ctime =  new Date().getTime(),
        	oneDay = 2000,
        	nowtime,
            flag = $stateParams.flag,
            cpuObj,
            cupChart={old:[]},
            netChart={old:[]},
            netOutChart={old:[]},
            memoryChart={old:[]},
            network,netsName=[];

        /* scope变量声明
         ---------------------------------------*/
        $rootScope.moduleTitle = '资源管理 > 主机';
        $scope.title = "主机详情";
        $rootScope.link = '/statics/css/common.css';//引入页面样式
        $scope.flag = flag;
        $scope.datamodel='static';

        /* UI事件绑定
         ---------------------------------------*/
        $scope.goBack =goBackFun;
        $scope.modelChange=modelChangeFun;


        /* 入口函数 （代码执行的唯一入口，也是起始点，位置可以根据具体情况调整）
         ---------------------------------------*/
        (function () {
            _init();
        })();

        /* UI事件实现
         ---------------------------------------*/
        function goBackFun(){
            _clearInterval();
            if(flag==0){
                $state.go('app.resources.host');
            }else{
                $state.go('app.resources.swarm');
            }
        }
        function modelChangeFun(str){
            var i=0,obj,s=0;
            cpuObj.model='change';
            cpuObj.type=str;
            _clearInterval();
            cupChart.old=[{data:[]}];
            netChart.old=[];
            netOutChart.old=[];
            memoryChart.old=[{data:[]}];
            if(str==='static'){
                _lineChar(cpuObj);
            }else{
            	nowtime=0;
            	for(s=0;s<netsName.length;s++){
            		netChart.old.push({data:[]});
	           		netOutChart.old.push({data:[]});
            	}
                for(i =-60; i < 0;i++){
                	obj=beforerandomData(i);
                	for(s=0;s<netsName.length;s++){
    	           		netChart.old[s].data.push(obj);
                    	netOutChart.old[s].data.push(obj);
                	}
                    cupChart.old[0].data.push(obj);
                    memoryChart.old[0].data.push(obj);
                }
                getDataCicle();
                window.timer=$interval(getDataCicle,5000);
            }
        }

        /* 内部方法实现
         ---------------------------------------*/
        function _init(){
            $scope.showDetail = $scope.isActive = true;
            httpLoad.loadData({
                url:'/runtime/host/detail',
                data: {hostId: id},
                success:function(data){
                    if(data.success&&data.data){
                        $scope.hostDetail = data.data;
                        $scope.showDetail = $scope.isActive = true;
                        network=data.data.network;
                        cpuObj={objectName:$scope.hostDetail.hostRealName,monitorType:'host'};
                        _lineChar(cpuObj);
                    }
                }
            });
        }
        function _clearInterval(){
            if(window.timer){
                $interval.cancel(window.timer);
            }
            delete window.timer;
        }

        /*线图数据*/
        function _lineChar(obj){
            var resultData,i,netInData=[],netOutData=[];
            httpLoad.loadData({
                url:'/monitor/monitor/history',
                data:{
                    objectName:obj.objectName,
                    monitorType:obj.monitorType
                },
                success:function(data){
                    if(data.success&&data.data){
                    	resultData=data.data;
                    	if(angular.toJson(resultData)==='{}'){
                    		$scope.resultTip=true; 
                    		return;
                    	}
                    	$scope.resultTip=false;  //是否提示无数据
                        //根据网络，分别得到入网出网数据
                        if(network.length>0){
                            for(i=0;i<network.length;i++){
                            	if(resultData['net_in['+network[i].name+']']){
                            		netsName.push(network[i].name);
                            		if(cpuObj.model==='change'){
                                        netInData.push(resultData['net_in['+network[i].name+']']);
                                        netOutData.push(resultData['net_out['+network[i].name+']']);
                                    }else{
                                        netInData.push(eChartLineFactory.initData(resultData['net_in['+network[i].name+']'],'net'));
                                        netOutData.push(eChartLineFactory.initData(resultData['net_out['+network[i].name+']'],'net'));
                                    }
                            	}
                            }
                        }
                        if(cpuObj.model==='change'){
                        	
                            cupChart.old=eChartLineFactory.changData({type:cpuObj.type,data:[resultData.cpu] });
                            netOutChart.old=eChartLineFactory.changData({type:cpuObj.type,data:netOutData,model:'net'});
                            netChart.old=eChartLineFactory.changData({type:cpuObj.type,data:netInData,model:'net'});
                            memoryChart.old=eChartLineFactory.changData({type:cpuObj.type,data:[resultData.mem_available],model:'mem'});
                            _changeChart(cupChart);
                            _changeChart(netChart);
                            _changeChart(netOutChart );
                            _changeChart(memoryChart);
                        }else{
                            
                            cupChart.Config=eChartLineFactory.setChartConfig({title:"CPU",data:[eChartLineFactory.initData(resultData.cpu)],seriesNames:['CPU']});
                            cupChart.id="img_cpu";
                            cupChart.type="static";
                            _drawChart(cupChart);


                            netChart.Config=eChartLineFactory.setChartConfig({title:"入网",data:netInData,seriesNames:netsName});
                            netChart.id="img_net";
                            netChart.type="static";
                            _drawChart(netChart);

                            netOutChart.Config=eChartLineFactory.setChartConfig({title:"出网",data:netOutData,seriesNames:netsName});
                            netOutChart.id="img_net_out";
                            netOutChart.type="static";
                            _drawChart(netOutChart);
                            
                            memoryChart.Config=eChartLineFactory.setChartConfig({title:"内存",data:[eChartLineFactory.initData(resultData.mem_available,'mem')],seriesNames:['内存']});
                            memoryChart.id="img_memery";
                            memoryChart.type="static";
                            _drawChart(memoryChart);

                        }
                    }else{
                    	$scope.resultTip=true;  //是否提示无数据
                    }
                }
            });
        }

        function _drawChart(chartConf){
            var myChart = echarts.init(document.getElementById(chartConf.id));
            chartConf.myChart=myChart;
            myChart.setOption(chartConf.Config);
        }
        function _changeChart(chartConf){
        	var i=1,series=[
    	            { data:angular.copy(chartConf.old[0].data) }
        	 ];
        	if(chartConf.old.length>1){
        		for(i=1;i<chartConf.old.length;i++){
        			series.push( { data:angular.copy(chartConf.old[i].data)} );
        		}
        	}
            chartConf.myChart.setOption({
                series:series
            });
        }
        function getDataCicle(){
        	var resultData,netInData=[],netOutData=[],i;
                httpLoad.loadData({
                    url:'/monitor/monitor/immediate',
                    data:{
                        objectName:cpuObj.objectName,
                        monitorType:cpuObj.monitorType
                    },
                    success:function(data){
                        if(data.success&&data.data){
                        	resultData=data.data;
                        	 //根据网络，分别得到入网出网数据
                            if(network.length>0){
                                for(i=0;i<network.length;i++){
                                	if(resultData['net_in['+network[i].name+']']){
                                		netInData.push(resultData['net_in['+network[i].name+']']);
                                        netOutData.push(resultData['net_out['+network[i].name+']']); 
                                	}
                                }
                            }
                            cupChart.old=eChartLineFactory.changData({old:cupChart.old,data:[resultData.cpu],type:'auto'});
                            netOutChart.old=eChartLineFactory.changData({old:netOutChart.old,data:netOutData,type:'auto',model:'net'});
                            netChart.old=eChartLineFactory.changData({old:netChart.old,data:netInData,type:'auto',model:'net'});
                            memoryChart.old=eChartLineFactory.changData({old:memoryChart.old,data:[resultData.mem_available],type:'auto',model:'mem'});
                            _changeChart(cupChart);
                            _changeChart(netChart);
                            _changeChart(netOutChart);
                            _changeChart(memoryChart);
                        }else{
                            _clearInterval();
                        }
                    }
                });
         }
        function beforerandomData(i) {
            nowtime = new Date(+ctime + oneDay*i).getTime();
            return {
                name:'oo',
                value: [
                    nowtime,
                    0
                ]
            };
        }

    }]);
})();
